﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.MVC.Community
{
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Community;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.Common.Web;
    using LdCms.Web.Models;
    using LdCms.Web.Services;
    using LdCms.Web.Authorizes;

    [AdminAuthorizeAttribute(Roles = "Admins")]
    public class CommunityThemeReplyController : BaseController
    {
        private readonly IBaseManager BaseManager;
        private readonly IColumnService ColumnService;
        private readonly IVThemeReplyService VThemeReplyService;
        public CommunityThemeReplyController(IBaseManager BaseManager, IColumnService ColumnService, IVThemeReplyService VThemeReplyService) : base(BaseManager)
        {
            this.BaseManager = BaseManager;
            this.ColumnService = ColumnService;
            this.VThemeReplyService = VThemeReplyService;
        }

        public override IActionResult Index()
        {
            return View();
        }

        public IActionResult List()
        {
            try
            {
                string funcId = PermissionEnum.CodeFormat((int)PermissionEnum.公告管理.公告信息.列表);
                if (!IsPermission(funcId)) { return ToPermission(funcId); }

                string startTime = GetQueryString("datemin");
                string endTime = GetQueryString("datemax");
                string columnId = GetQueryString("columnId");
                string state = GetQueryString("state");
                string keyword = GetQueryString("keyword");
                ViewData["DateMin"] = startTime;
                ViewData["DateMax"] = endTime;
                ViewData["ColumnId"] = columnId;
                ViewData["State"] = state;
                ViewData["Keyword"] = keyword;

                int total = 100;
                List<V_Community_ThemeReply> lists = new List<V_Community_ThemeReply>();
                string strKeyword = string.Format("{0}{1}{2}{3}", startTime, columnId, state, keyword);
                if (string.IsNullOrWhiteSpace(strKeyword))
                    lists = VThemeReplyService.GetThemeReplyTop(SystemID, CompanyID, total);
                else
                    lists = VThemeReplyService.SearchThemeReply(SystemID, CompanyID, startTime, endTime, columnId, state, keyword, total);
                ViewData["Count"] = VThemeReplyService.CountThemeReply(SystemID, CompanyID);
                return View(lists);
            }
            catch (Exception ex)
            {
                return ToError(ex.Message);
            }
        }
        public IActionResult Add(string themeId,string replyId)
        {
            try
            {
                string funcId = string.Empty;
                if (!IsAddPermission(themeId, out funcId)) { return ToPermission(funcId); }

                if (string.IsNullOrWhiteSpace(themeId))
                    return View(new V_Community_ThemeReply());
                var entity = VThemeReplyService.GetThemeReply(SystemID, CompanyID, themeId, replyId);
                if (entity == null)
                    return View(new V_Community_ThemeReply());
                return View(entity);
            }
            catch (Exception ex)
            {
                return ToError(ex.Message);
            }
        }

        #region 辅助方法
        [HttpGet]
        [ActionName("column-list-all")]
        public JsonResult GetColumnAll()
        {
            try
            {
                var lists = ColumnService.GetColumnAll(SystemID, CompanyID);
                if (lists == null)
                    return Error("not data！");
                var data = from m in lists
                           select new
                           {
                               id = m.ColumnID,
                               name = m.ColumnName
                           };
                return Success("ok", data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 私有化方法
        public bool IsSavePermission(string noticeId)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(noticeId))
                {
                    string funcId = PermissionEnum.CodeFormat((int)PermissionEnum.公告管理.公告信息.新增);
                    return IsPermission(funcId) ? true : false;
                }
                else
                {
                    string funcId = PermissionEnum.CodeFormat((int)PermissionEnum.公告管理.公告信息.编辑);
                    return IsPermission(funcId) ? true : false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool IsAddPermission(string noticeId, out string funcId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(noticeId))
                {
                    funcId = PermissionEnum.CodeFormat((int)PermissionEnum.公告管理.公告信息.新增);
                    return IsPermission(funcId) ? true : false;
                }
                else
                {
                    funcId = PermissionEnum.CodeFormat((int)PermissionEnum.公告管理.公告信息.编辑);
                    return IsPermission(funcId) ? true : false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}